import request from '@/utils/request'
import {
  urlParam
} from '@/utils/index'

export function saveQuestion(question) {
  let questionInfo = JSON.parse(JSON.stringify(question))
  if(questionInfo.answer instanceof Array){
    questionInfo.answer = JSON.stringify(questionInfo.answer)
  }
  if(questionInfo.subjectId instanceof Array){
    questionInfo.subjectId = questionInfo.subjectId[questionInfo.subjectId.length - 1]
  }
  return request.post('/question', questionInfo)
}

export function onlineQuestion(questionId,onlineStatus) {
  return request.put('/question/'+questionId+':online?onlineStatus='+onlineStatus)
}


export function passQuestion(batchNo) {
  return request.put('/spotCheck/batchNo:' + batchNo + "/pass")
}

export function rejectQuestion(batchNo, msg) {
  return request.put('/spotCheck/batchNo:' + batchNo + "/reject?msg=" + msg)
}

export function queryBatch(query) {
  if (query.time && query.time.length) {
    query.startTime = query.time[0]
    query.endTime = query.time[1]
  } else {
    query.startTime = ''
    query.endTime = ''
  }
  return request.get('/spotCheck/batch?' + urlParam(query))
}

export function checkingQuestion(id, status) {
  return request.put('/spotCheck/' + id + "?status=" + status)
}

export function queryUpAndDown(batchNo, sampleId) {
  return request.get('/spotCheck/batchNo:' + batchNo + '/upAndDown?id=' + sampleId)
}

export function revokeQuestion(batchNo, sampleId) {
  return request.post('/spotCheck/batchNo:' + batchNo + '/revoke')
}

export function addSpotCheck(param) {
  if (param.time && param.time.length) {
    param.startTime = param.time[0]
    param.endTime = param.time[1]
  } else {
    param.startTime = ''
    param.endTime = ''
  }
  return request.post('/spotCheck', param)
}

export function addSpotCheckAgain(batchNo){
  return request.post('/spotCheck//batchNo:'+batchNo)
}

export function getSpotCheck(param) {
  if (param.time && param.time.length) {
    param.startTime = param.time[0]
    param.endTime = param.time[1]
  } else {
    param.startTime = ''
    param.endTime = ''
  }
  return request.get('/spotCheck?' + urlParam(param))
}

export function spotCheckQuestionSum(query) {
  if (query.time && query.time.length) {
    query.startTime = query.time[0]
    query.endTime = query.time[1]
  } else {
    query.startTime = ''
    query.endTime = ''
  }
  return request.get('/question/spotCheckSum?' + urlParam(query))
}

export function spotCheckQuestionList(query) {
  if (query.time && query.time.length) {
    query.startTime = query.time[0]
    query.endTime = query.time[1]
  } else {
    query.startTime = ''
    query.endTime = ''
  }
  return request.get('/question/spotCheck?' + urlParam(query))
}

export function saveQuestionInfoLog(questionChangeLogId, logInfo) {
  return request.post('/question/log', {
    questionChangeLogId: questionChangeLogId,
    questionChangeInfoLogs: logInfo
  })
}

export function searchQuestionList(title) {
  return request.get('/question/search?title=?' + title)
}

export function questionList(question) {
  return request.get('/question?' + urlParam(question))
}

export function questionInfo(id) {
  return request.get(`/question/id:${id}`)
}

export function commitCheck(questionId, checkPeople) {
  const fd = new FormData()
  fd.set('checkPeople', checkPeople)
  return request.put(`/question/${questionId}/commitCheck`, fd)
}

export function rejectCheck(questionId, msg) {
  return request.put(`/question/${questionId}/rejectCheck?msg=${msg}`)
}

export function commitReview(questionId, reviewPeople) {
  const fd = new FormData()
  fd.set('reviewPeople', reviewPeople)
  return request.put(`/question/${questionId}/commitReview`, fd)
}

export function rejectReview(questionId, msg) {
  return request.put(`/question/${questionId}/rejectReview?msg=${msg}`)
}

export function review(questionId) {
  return request.put(`/question/${questionId}/review`)
}

export function questionSort(questionId, sort) {
  return request.put(`/question/${questionId}/sort?sort=${sort}`)
}

export function deleteQuestion(questionId) {
  return request.delete(`/question/${questionId}`)
}

export function questionUpAndDown(questionId, activeName) {
  return request.get(
    `/question/questionUpAndDown?questionId=${questionId}&activeName=${activeName}`
  )
}

export function delQuestionAll(ids) {
  return request.delete("/question/delQuestionAll", {
    data: {
      ids: ids
    }
  });
}

export function rejectCheckMultiple(ids, msg) {
  return request({
    url: '/question/rejectCheckMultiple',
    method: 'post',
    data: {
      ids,
      msg
    }
  })
}

export function imageQuestionWorkflow(fileUrl, subjectId) {
  return request({
    url: '/question/imageQuestionWorkflow',
    method: 'get',
    params: {
      fileUrl,
      subjectId
    }
  })
}

// AI解析题目
export function aiAnalyzeQuestion(questionTitle, questionType, materials, select) {
  const data = {
    questionTitle,
    type: questionType
  }
  
  // 材料有就填
  if (materials) {
    data.materials = materials
  }
  
  // 选择题、判断题等有选项的需要传
  if (select && select.length > 0) {
    data.select = select
  }
  
  return request({
    url: '/question/aiAnalyze',
    method: 'post',
    data
  })
}

// AI判断知识点
export function aiJudgeKnowledge(question, knowledgePoints) {
  const data = {
    question,
    knowledgePoints
  }
  
  return request({
    url: '/question/aiJudgeKnowledge',
    method: 'post',
    data
  })
}
